package com.evan.wj.service;

import com.evan.wj.dao.BookshelfDao;
import com.evan.wj.pojo.Bookshelf;
import com.evan.wj.pojo.ViewBS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BookshelfService {
    @Autowired
    BookshelfDao bookshelfDao;

    public List<ViewBS> personalBookshelf(int userId){
        ArrayList<ViewBS> viewBS = new ArrayList<>();
        for (Object[] objects : bookshelfDao.findPersonalAllBook(userId)) {
//            for ( int  i = 0; i < objects.length; i++){
//                System.out.println(objects[i]);
//            }
            ViewBS viewBs = new ViewBS();
            viewBs.setId(viewBS.size());
            viewBs.setUserId((Integer) objects[0]);
            viewBs.setBookId((Integer) objects[1]);
            viewBs.setCover((String) objects[2]);
            viewBs.setTitle((String) objects[3]);
            viewBs.setContentId((Integer) objects[4]);
            viewBS.add(viewBs);
        }
        return viewBS;
    }

    public List<ViewBS> list(){
        ArrayList<ViewBS> viewBS = new ArrayList<>();
        for (Object[] objects : bookshelfDao.findPersonalAllBook()) {
//            for ( int  i = 0; i < objects.length; i++){
//                System.out.println(objects[i]);
//            }
            ViewBS viewBs = new ViewBS();
            viewBs.setId(viewBS.size());
            viewBs.setUserId((Integer) objects[0]);
            viewBs.setBookId((Integer) objects[1]);
            viewBs.setCover((String) objects[2]);
            viewBs.setTitle((String) objects[3]);
            viewBs.setContentId((Integer) objects[4]);
            viewBS.add(viewBs);
        }
        return viewBS;
    }

    public void delete(Bookshelf bookshelf){
        bookshelfDao.delete(bookshelf);
    }


    public void add(Bookshelf bookshelf){

        Bookshelf save = bookshelfDao.save(bookshelf);
    }

}
